package com.fushenlan.epidemicControl.controller;

import cn.hutool.core.util.StrUtil;
import com.fushenlan.Vo.EmergencyInfoVo;
import com.fushenlan.commons.ApiGlobalErrorDefinition;
import com.fushenlan.commons.GeneralResponse;
import com.fushenlan.epidemicControl.config.JwtUtil;
import com.fushenlan.epidemicControl.dto.EmergencyInfoDto;
import com.fushenlan.epidemicControl.service.EmergencyInfoService;
import com.fushenlan.po.NeighborhoodMember;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Author XUJIN
 * @DATE 2022/4/21
 * @PACKAGE_NAME com.fushenlan.epidemicControl.controller
 * @VERSION 1.0
 * 紧急求助控制器
 */
@Slf4j
@RestController
@RequestMapping("/emergency")
@Api(value = "紧急求助管理控制器", description = "紧急求助管理控制器", tags = "emergency-controller")
public class EmergencyController {

    @Autowired
    private EmergencyInfoService emergencyInfoService;

    private static final String NEIGHOBRHOOD_ID = "neighborhoodId";  //当前登录用户所属居委会id

    @GetMapping("/findAllEmergencyList")
    @ApiOperation(value = "求助信息列表")
    public GeneralResponse findAllEmergencyList(EmergencyInfoDto emergencyInfoDto, int pageNo, int pageSize) {
        // TODO: 2022/4/23 分页待完善
        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        emergencyInfoDto.setNeighborId(Integer.valueOf(userInfo));
        try {
            return emergencyInfoService.findAllEmergencyList(emergencyInfoDto,pageNo,pageSize);
        }catch (Exception e){
            log.error("获取失败====>{}",e);
            return GeneralResponse.fail();
        }
    }

    @GetMapping("/getEmergencyInfoById")
    @ApiOperation(value = "根据id查看求助信息")
    public GeneralResponse getEmergencyInfoById(@ApiParam(value = "求助信息表id", required = true) @RequestParam Integer id) {
        // TODO: 2022/4/23 分页待完善
        try {
            log.info("params = [id:{}]",id);
            EmergencyInfoVo emgs = emergencyInfoService.getEmergencyInfoById(id);
            return GeneralResponse.success(emgs);
        } catch (Exception e) {
            log.error("获取求助信息异常 = {}",e);
            return GeneralResponse.fail();
        }

    }

    @PostMapping("/dealEmergency")
    @ApiOperation(value = "处理求助信息/分配紧急度")
    public GeneralResponse dealEmergency(@RequestBody EmergencyInfoDto emergencyInfoDto) {
        // TODO: 2022/4/23 分页待完善
        return  emergencyInfoService.dealEmergency(emergencyInfoDto);
    }

    /**
     * 获取当前登录居委会下属所有居委会成员
     * @return
     */
    @GetMapping("/getNeighborMember")
    @ApiOperation(value = "获取所有的居委工作人员列表")
    public GeneralResponse getNeighborMember() {
        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        if (StrUtil.isEmpty(userInfo)){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.TOKEN_EXPIRED);
        }
        List<NeighborhoodMember> neighborMember = emergencyInfoService.getNeighborMember(Integer.valueOf(userInfo));
        return GeneralResponse.success(neighborMember);
    }


    /**
     *
     * @return
     */
    @GetMapping("/exportEmergencyInfo")
    @ApiOperation(value = "导出求助信息列表")
    public void  exportEmergencyInfo(HttpServletResponse response) {

        String userInfo = JwtUtil.getUserInfo(NEIGHOBRHOOD_ID);
        if (StrUtil.isEmpty(userInfo)){
            return;
        }
        // 导出数据，数据，数据类型，响应对象文件名称
        emergencyInfoService.exportEmergencyInfo(Integer.valueOf(userInfo),response);
    }

    /**
     * 对求助任务批量分配处理人
     * @return
     */
    @PostMapping("/batchAssign")
    public GeneralResponse batchAssignDealUser(@RequestBody EmergencyInfoDto emergencyInfoDto){

        log.info("params = 【dealId:{},ids:{},level:{}】",emergencyInfoDto.getDealId(),emergencyInfoDto.getIds(),emergencyInfoDto.getLevel());
        if (StringUtils.isEmpty(emergencyInfoDto.getIds())){
            return GeneralResponse.fail(ApiGlobalErrorDefinition.NOT_SELECT);
        }
        return emergencyInfoService.batchAssignDealUser(emergencyInfoDto.getDealId(),emergencyInfoDto.getLevel(),emergencyInfoDto.getIds());

    }




}
